import UcUtils from './ucUtils'
import Vue from 'vue'
import {BootstrapVue} from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import ucConfig from './ucConfig'
import UcClient from './client/ucClient'
import UserRegisterRequest,{UserRegisterResponse} from './client/userRegister'
import UserInfoRequest, { UserInfoResponse } from './client/userInfo'
import UserSynLoginRequest, {UserSynLoginResponse} from './client/userSynLogin'
import UserSynLogoutRequest, { UserSynLogoutResponse } from './client/userSynLogout'
import UserCheckUsernameRequest, { UserCheckUsernameResponse } from './client/userCheckUserName'
import UserCheckEmailRequest, { UserCheckEmailResponse } from './client/userCheckEmail'

Vue.use(BootstrapVue)
let app = new Vue({
    data: {
        encoderTypes:[
            {text:'加密', value:'encode'},
            {text:'解密', value:'decode'}
        ],
        encoder:{
            type:'encode',
            ucKey:'',
            code:'',
            result:'',
        },
    },
    created(){
        this.encoder.ucKey = ucConfig.ucKey // 默认值
    },
    mounted() {

    },
    methods:{
        runEncoder(){
            let ucEncoder = UcUtils.getUcEncoder(this.encoder.ucKey),result = ''
            try{
                if(this.encoder.type === 'encode'){
                    result = ucEncoder.encode(this.encoder.code)
                }else{
                    result = ucEncoder.decode(this.encoder.code)
                    if(result === ''){
                        result = '解密失败！'
                    }
                }
            }catch(err){
                result = '操作失败！'+err.message
            }
            this.encoder.result = result
        },
        /**
         * 检查用户名是否可用
         */
        checkUsername(){
            let username = 'dc';
            let req = new UserCheckUsernameRequest(username),
                client = new UcClient(ucConfig);
            client.request(req).then(respRaw=>{
                let resp = new UserCheckUsernameResponse(respRaw);
                console.log(`UserCheckUsername: ${username} is ${resp.isSuccess?'':'in'}valid`, resp.errMsg, resp)
            }).catch(err=>{
                console.log('UserCheckUsername Error', err)
            })
        },
        /**
         * 检查邮箱是否可用
         * 
         * @ignore 暂时用不上
         */
        checkEmail(){
            let email = 'a@b.c';
            let req = new UserCheckEmailRequest(email),
            client = new UcClient(ucConfig);
            client.request(req).then(respRaw=>{
                let resp = new UserCheckEmailResponse(respRaw);
                console.log(`UserCheckEmail: ${email} is ${resp.isSuccess?'':'in'}valid`, resp.errMsg, resp)
            }).catch(err=>{
                console.log('UserCheckEmail Error', err)
            })
        },
        /**
         * 注册UCenter账户
         * 
         * 同时也是注册论坛账户
         */
        userRegister(){
            let username = 'chiang', password = '123456', email = 'chiang@local.host',
            regReq = new UserRegisterRequest(username, password, email),
            client = new UcClient(ucConfig)
            return client.request(regReq).then(resp=>{
                let regResp = new UserRegisterResponse(resp)
                console.log('regReq isSuccess',regResp.isSuccess,'errCode',regResp.errCode,'errMsg',regResp.errMsg, 'rawData', regResp._rawData,'regResp',regResp)
            }).catch(err=>{
                console.log('regReq 出错啦！', err.message, 'err', err)
            })
        },
        /**
         * 获取UCenter账户信息
         * 
         * @ignore 暂时用不上
         */
        userInfo(){
            let userId = 1;
            let uInfoReq = new UserInfoRequest(userId, 1),
            client = new UcClient(ucConfig)
            return client.request(uInfoReq).then(resp=>{
                let uInfoResp = new UserInfoResponse(resp)
                console.log('uInfoResp', uInfoResp)
            }).catch(err=>{
                console.log('uInfoReq err', err)
            })
        },
        /**
         * 同步登陆
         */
        userSynLogin(uid){
            let userId = 8,
                loginReq = new UserSynLoginRequest(userId),
                client = new UcClient(ucConfig);
            client.request(loginReq).then(resp=>{
                let loginResp = new UserSynLoginResponse(resp)
                if(loginResp.isSuccess){
                    // loginResp.loginUrl获取的地址后面添加参数jumpTo，可以在同步登陆后自动跳转到指定的地址（需要论坛端/uc/api.php支持）
                    let synLoginUrl = loginResp.loginUrl+'&jumpTo=' + encodeURIComponent(ucConfig.bbsAppUrl);
                    console.log('syn login success', synLoginUrl, loginResp)
                    // window.location.href = synLoginUrl;
                }else{
                    console.log('syn login err', loginResp.errMsg, loginResp)
                }
            }).catch(err=>{
                console.log('syn login err', err)
            })

        },
        /**
         * 同步退出
         */
        userSynLogout(){
            let logoutReq = new UserSynLogoutRequest(),
                client = new UcClient(ucConfig);
            return client.request(logoutReq).then(resp=>{
                let logoutResp = new UserSynLogoutResponse(resp);
                if(logoutResp.isSuccess){
                    let homeSite = 'http://my.app.local/index.html'; // 演示地址，应用主站
                    let synLogoutUrl = logoutResp.logoutUrl+'$jumpTo=' + encodeURIComponent(homeSite);  // 演示退出登陆后回到主站
                    console.log('syn logout success', synLogoutUrl, logoutResp)
                    // window.location.href = synLogoutUrl;
                }else{
                    console.log('syn logout err', logoutResp.errMsg,logoutResp)
                }
            }).catch(err=>{
                console.log(err)
            })
        }
    }
    // render:h=>h(App)
})
app.$mount('#app')